package com.cnswhy.cloud.server.system.controller;

import com.cnswhy.cloud.common.core.base.R;
import com.cnswhy.cloud.common.core.utils.CnswhyUtil;
import com.cnswhy.cloud.server.system.entity.SystemUser;
import com.cnswhy.cloud.server.system.feign.SystemUserApi;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.security.Principal;
import java.util.HashMap;
import java.util.Map;

@Slf4j
@RestController
public class TestController {

    @GetMapping("info")
    public R<String> test(){
        return R.success("cnswhy-server-system");
    }
    @Resource
    SystemUserApi systemUserApi;
    @GetMapping("abc")
    public R<Boolean> abc(){
        SystemUser systemUser = new SystemUser();
         systemUserApi.save(systemUser);
        return R.successDef();
    }

    @GetMapping("currentUser")
    public Principal currentUser(Principal principal) {
        return principal;
    }

    @GetMapping("hello")
    public String hello(String name) {
        log.info("/hello服务被调用");
        return "hello" + name;
    }


    /**
     * 获取当前用户信息
     */
    @GetMapping("user")
    public R<Map<String, Object>> currentUser() {
        Map<String, Object> map = new HashMap<>(5);
        map.put("currentUser", CnswhyUtil.getCurrentUser());
        map.put("currentUsername", CnswhyUtil.getCurrentUsername());
        map.put("currentUserAuthority", CnswhyUtil.getCurrentUserAuthority());
        map.put("currentTokenValue", CnswhyUtil.getCurrentTokenValue());
        map.put("currentRequestIpAddress", CnswhyUtil.getHttpServletRequestIpAddress());
        return R.success(map);
    }
}
